<?php
namespace app\index\controller;
use think\Controller;
use think\Config;
use app\index\domain as domain;

class Index extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(IS_POST)
            if(I('?get.form'))
                if(A('Form')->add())
                    $this->assign('formOK', true);
                else
                    $this->assign('formOK', false);
        //domain\Classes::cacheStart('test');
        $site = domain\Classes::get('Option');
        $links = domain\Classes::get('Links');
        $termList = domain\Classes::get('TermList', 0);
        $this->view->assign('termList',$termList);
        $this->view->assign('site',$site);
        $this->view->assign('links',$links);
		
		
		
		if(Config::get('turnon')==1){
			//同一个IP在两分钟内不重复记录,同一个IP通过不同方式访问则分别记录
			$where['ip']= get_client_ip();
			$info = M('visitor')->where($where)->order('id desc')->find();
			$result = (time() - $info['stamp'])>600;
			$type = $info['type'];
			$new_type = get_referer();
			if($result || ($new_type != $type)){
				$data['time']=date('Y-m-d-H',time());
				$data['date']=date('n-j',time());
				$data['hours']=date('G',time());
				$data['stamp']=time();
				$data['ip']=get_client_ip();
				$data['type']=get_referer();
				$data['color']=randrgb();
				$data['url']=isset($_SERVER["HTTP_REFERER"])?$_SERVER["HTTP_REFERER"]:get_url();
				$add = M('visitor')->add($data);
			}
			
			//计算浏览量 而不是访问量 这样显得数据量多,以天为单位
			$he =M('count');
			$day['time']=date('Y-m-d-G',time());
			$find_day = $he->where($day)->count();
			if($find_day == 0){
			 $num['num']=1;
			 $num['time']=date('Y-m-d-G',time());
			 $num['hours']=date('G',time());
			 $num['days']=date('n-j',time());
			 $add_day = $he->add($num); 
			}else{
			 $there['time']=date('Y-m-d-G',time());
			 $sum = $he->where($there)->setInc('num');
			}
			      //删除一定数量的数据表记录 暂不做
			//END
		}
		
		//
		$this->content = M('content');
	    $this->product = M('product');	
	    $this->links = M('links');
	    $this->term = M('term');		
    }
    
    public function __destruct()
    {
        //domain\Classes::cacheEnd('test');
    }

    /**
     * 显示首页
     */
    public function index()
    {

    //dump(VENDOR_PATH);
   //Vendor('Wxpay.lib.WxPay#Api');  
		/**
		 *  筛选
		 */
		$filter = $this->links;
		$area = $filter->where('lid=21')->order('id asc,listorder desc')->select();
		$this->assign('area',$area);
		$prise = $filter->where('lid=22')->order('id asc,listorder desc')->select();
		$this->assign('prise',$prise);		
		$layout = $filter->where('lid=23')->order('id asc,listorder desc')->select();
		$this->assign('layout',$layout);		
	     /**
	      *  recommend
	      */
		$rec =  $this->term->where('display_nav=1')->order('listorder desc,id desc')->select();
		$new_rec = $this->json_array($rec,count($rec)-1);
		$this->assign('recommend',$new_rec);	
		/**
		*    stick
		*/
        $stick=$this->content->where('rec_ok=1')->select();
        $this->assign('stick',$stick);
    /**
    *   lunbo
    */
       $lunbo = $filter->where('lid=1')->select();
       $this->assign('lunbo',$lunbo);

        return $this->fetch();
    }

  /**
   *  新闻内容，news_list,news_content
   */
    public function news_list()
    {
    	if($_GET['nid'] == 1){
    	  $where['pinzhi']=$_GET['nid'];	
    	}else if($_GET['nid'] == 2){
    	  $where['repan']=$_GET['nid'];
    	}else{
    	  $where['rec_ok']=1;
    	}

    	$news_pinzhi = $this->content->where($where)->select();
    	$this->assign('news_pinzhi',$news_pinzhi);
    	return $this->fetch();
    }
	
   public function news_content($id)
   {
    $info = M('content')->where('id='.$id)->find();
    $this->assign('info',$info);
    //相关阅读 ，除了这个的其他的前三个
    $where['id']=array('neq',$id);
    $relation = M('content')->where($where)->select();
    $this->assign('list',$relation);
    return $this->fetch();
   }

   /**
   *  楼盘内容，loupan_list,loupan_content,loupan_xiangce
   */

	public function loupan_list()
	{
    		//判断搜索
    			$where['display_nav']=1;

          $area =isset($_POST['area'])?$_POST['area']:"";
          $junjia =isset($_POST['junjia'])?$_POST['junjia']:"";
          $huxing =isset($_POST['huxing'])?$_POST['huxing']:"";

    			isset($_POST['area'])?$where['area']=array('like',"%$area%"):"";
    			isset($_POST['junjia'])?$where['junjia']=array('like',"%junjia%"):"";
    			isset($_POST['huxing'])?$where['huxing']=array('like',"%$huxing%"):"";	


	        $list = $this->term->where($where)->select();
	        if(!empty($list)){
	          $new_array = $this->json_array($list,count($list)-1); 	
	        }else{
	          $new_array = $this->term->where($where)->select();	
	        }
	        $this->assign('list',$new_array);
         //判断搜索 END
		return $this->fetch();
	}


	public function loupan_content($id)
	{

  		$loupan = $this->term->where('id='.$_GET['id'])->find();
      $info = $this->json_array($loupan,count($loupan)-1);
       session('loupan_name',$info['name']);
      if(isset($info['duotu'])){
        $duotu = $info['duotu'];
      }else{
        $duotu=array();
      }
     
      $this->assign('duotu',$duotu);
  		$this->assign('info',$info);
  		//点评个数及最新点评
  		$dianping = M('message');
      $what['panid']=$id;
      $what['message_type']=1;      
  		$num =$dianping->where($what)->count();
  		$new_message = $dianping->where($what)->order('id desc')->find();
      //会员信息
      if($new_message){
        $m_info = M('member')->where('id='.$new_message['mid'])->find();
      }else{
        $m_info = M('member')->where('id=0')->find();
      }
      $this->assign('m_info',$m_info);
  		$this->assign('new_message',$new_message);
      $this->assign('num',$num);
      //最新动态，按照rec_ok推荐的最新新闻
      $new_dongtai = $this->content->where('rec_ok=1')->order('id desc')->find();
      $this->assign('new_dongtai',$new_dongtai);
      //主力户型
      $zhuli_huxing = $this->product->where('term_id='.$id)->select();
      $huxing_num =count($zhuli_huxing);
      $this->assign('huxing_num',$huxing_num);
      $this->assign('zhuli_huxing',$zhuli_huxing);

      //楼盘，猜你喜欢，标准：其他楼盘
       $where['display_nav']=1;
       $where['id']=array('neq',$_GET['id']);
       $list = $this->term->where($where)->order('listorder desc,id desc')->select();
       $clist = $this->json_array($list,count($list)-1); 
       $this->assign('clist',$clist);
       //是否还有优惠券
       $map['lock']=0;
       $map['panid']=$_GET['id'];
       $have = M('coupon')->where($map)->count();
       $this->assign('have',$have);
       //只记录浏览的楼盘，通过获取id，再与term表中查找，浏览loupan_content一次记录一次。形成浏览记录
       //将字符串存进去,session字符串，进入浏览历史时再更新[这样会出现丢失的现象]
       //考虑写到文件中
       session('id'.$_GET['id'],$_GET['id']);
        $str="";
        for($i=1;$i<=30;$i++){
          if(session('id'.$i) != null){
            $str.= session('id'.$i).",";    
           }
        }
        session('str_id'.session('mid'),$str);
        //楼盘中的关注按钮状态,查concern,如果这个id在hid中证明已关注,
        if(session('mid')){
          $memid['userid']=session('mid');
          $loupan_id = M('concern')->where($memid)->getfield('hid');
          $exist = strpos("$loupan_id",$id);
            if($exist){
              $this->assign('status',1);
            }else{
              $this->assign('status',0);
            }
        }else{
          $this->assign('status',0);
        }
        //电话次数
        $ci = M('telcount')->where('panid='.$id)->find();
        $this->assign('cishu',$ci['cishu']);
	      return $this->fetch();
	}
 //拨打电话次数
public function bodacishu(){
  //先写到数据库，用延迟写入,根据楼盘id 记录次数
   $data['panid']=I('post.panid');
   $find=M('telcount')->where($data)->find();
  if($find){
    M('telcount')->where($data)->setInc('cishu',1,60);
  }else{
    $data['cishu']=10;
    M('telcount')->add($data);
  }

}

	public function loupan_dianping($id)
	{
       $what['panid']=$id;
       $what['message_type']=1;    
       $info = M('message')->where($what)->order('id desc')->select();
       $this->assign('info',$info);
		   return $this->fetch();
	}
    
    public function loupan_review($id)
    {
       $this->isLogin();
      if(IS_POST){
	      $info = M('message');
	      $info->create();
        $info->panid = $id;         	      
	      $info->create_time = time(); 
	      $info->add();
	      if($info){
	      	return $this->redirect('Index/loupan_dianping',array('id'=>$id));
	      }
      }
      return $this->fetch();
    }


	public function loupan_xiangce()
	{

      //循环出所有的相册类别
      $type = M('albumtype')->select();
      $this->assign('type',$type);
		  $album = M('album');
	   	//视频
       $video = $album->where('lid=1')->select();
       $this->assign('video',$video);
       //效果图
       $drawing= $album->where('lid=2')->select();
       $this->assign('drawing',$drawing);
       //样板图
        $model= $album->where('lid=3')->select();
       $this->assign('model',$model); 
       //实景图
         $map= $album->where('lid=4')->select();
       $this->assign('map',$map);            
       //户型图
         $layout= $album->where('lid=5')->select();
       $this->assign('layout',$layout); 
       //是否还有优惠券
       $map['lock']=0;
       $map['panid']=$_GET['id'];
       $have = M('coupon')->where($map)->count();
       $this->assign('have',$have);
		
        return $this->fetch();
	}
    
    public function video()
    {
        $info = M('album')->where('id='.$_GET['id'])->find();
        $this->assign('info',$info);
    	return $this->fetch();
    }
   /**
   *  户型相关内容
   */
  public function huxing_list($hxid)
  {

    if(IS_POST){
      $get= $_POST['jushi'];
      $shaixuan['term_id']=$hxid;
      $shaixuan['huxing'] =array('like',"%$get%");
      $info= M('product')->where($shaixuan)->select();
    }else{
     $info= M('product')->where('term_id='.$hxid)->select();    
   }
    $list = $this->json_array($info,count($info)-1);
    $this->assign('list',$list);
    //居室类型
    $jshi = M('links')->where('lid=29')->select();
    $this->assign('jshi',$jshi);
    return $this->fetch();
  }
  public function huxing_content($id,$hxid)
  {
    //id 指楼盘id，hxid 指product中具体户型的id
  	$info = M('product')->where('id='.$hxid)->find();
  	$fang = $this->json_array($info,count($info)-1);
      if(isset($fang['duotu'])){
        $duotu = $fang['duotu'];
      }else{
        $duotu=array();
      }
    $this->assign('fang',$fang);
    $this->assign('listpig',$duotu);
    //电话拨打次数
    $ci = M('telcount')->where('panid='.$id)->find();
    $this->assign('cishu',$ci['cishu']);
      //点评个数及最新点评
      $dianping = M('message');
      $whatsmsg['panid']=$id;
      $whatsmsg['message_type']=1;
      $num =$dianping->where($whatsmsg)->count();
      $what['panid']=$id;
      $what['message_type']=1;
      $new_message = $dianping->where($what)->order('id desc')->find();
      //会员信息
      if($new_message){
        $m_info = M('member')->where('id='.$new_message['mid'])->find();
      }else{
        $m_info = M('member')->where('id=0')->find();
      }
      
      $this->assign('m_info',$m_info);
      $this->assign('new_message',$new_message);
      $this->assign('num',$num);

    //最新动态，按照rec_ok推荐的最新新闻
    $new_dongtai = $this->content->where('rec_ok=1')->order('id desc')->find();
    $this->assign('new_dongtai',$new_dongtai); 

    //调用楼盘出的销售电话,term表中，指id
    $loupan = $this->term->where('id='.$_GET['id'])->find();
    $info = $this->json_array($loupan,count($loupan)-1);
    $this->assign('info',$info);

    //楼盘，猜你喜欢，标准：其他楼盘
     $where['display_nav']=1;
     $where['id']=array('neq',$_GET['id']);
     $list = $this->term->where($where)->order('listorder desc,id desc')->select();
     $clist = $this->json_array($list,count($list)-1); 
     $this->assign('clist',$clist);
      //是否还有优惠券
       $map['lock']=0;
       $map['panid']=$_GET['id'];
       $have = M('coupon')->where($map)->count();
       $this->assign('have',$have);

  	return $this->fetch();
  }


   /**
   *  秒杀，秒杀提交订单
   */	
  public function seckill($id)
  {
    $this->isLogin();
    $loupan = M('term')->where('id='.$id)->find();
    session('loupan',$loupan['name']);
    $where['panid']=$id;
    $info = M('coupon')->where($where)->find();
    session('diyongjiage',$info['money']);
    $this->assign('info',$info);

  	return $this->fetch();
  }

  public function confirm($id)
  {    
    $ifind['his']=session('mid');
    $ifind['panid']=$id;
    //判断是否存在未支付订单
    $ifind['status']=0;
    $if_find = M('coupon')->where($ifind)->find();
    if(!$if_find){
      $map['panid']=$id;
      $map['lock']=0;
      $new_code = M('coupon')->where($map)->select();

      $key = array_rand($new_code);
      $code = $new_code[$key]['code'];
      session('givcode',$code);
      //如果没有就将信息写入，有的话不执行数据库操作
      $data['lock']=1;
      $data['his']=session('mid');
      $where['code']=$code;
      $info = M('coupon')->where($where)->setField($data);           
    }else{
      //如果session，消失，就会报错,这里需要重新计算
       $code =session('givcode'); 
       if($code){
        $fc['his']=session('mid');
        $fc['lock']=1;
        $find_code = M('coupon')->where($fc)->find();
        $code = $find_code['code'];
        }
    }

  	return $this->fetch();
  }
  public function get_cod_nums()
{
        //查找表中是否有这个会员的id,没有就跟新一个，否则。不更新。随机抽取此楼盘下的一张优惠券
      $id = I('post.id');
      $type=I('post.type');


      if($type ==1){
         //根据code，分别将code数组写入到数据库，同时将lock变为1；
        $map['panid']=$id;
        $map['lock']=0;
        $new_code = M('coupon')->where($map)->select();       
          $key = array_rand($new_code);
          $code = $new_code[$key]['code'];
          $data['lock']=1;
          $data['his']=session('mid');
          $where['code']=array('in',$code);
          $info = M('coupon')->where($where)->setField($data);   
       }elseif($type == 0){
        //减号时在lock=1 ,his=mid批量更新 his，lock为0，即可实现释放优惠券
        $map['panid']=$id;
        $map['lock']=1;
        $new_code = M('coupon')->where($map)->select();         
           $del_key = array_rand($new_code);
           $del_code = $new_code[$del_key]['code'];
          $data['lock']=0;
          $data['his']=0;
          $where['code']=$del_code;     
          $info = M('coupon')->where($where)->setField($data); 
     }


}

  public function zprise()
  {

    $geshu =$_POST['geshu'];//个数
    $zongjia =$_POST['zongjia'];//总价
    //$code =$_POST['code'];//优惠码
    session('geshu',$geshu);
    session('zongjia',$zongjia);
    //根据优惠码，更新订单状态
    $where['code']=$code;
    $data['lock']=1;
    $data['his']=session('mid');
    $data['nums']=$geshu;
    $data['zongjia']=$zongjia;
   // M('coupon')->where($where)->save($data);
  }

  public function zhifu()
  {
    
    if(IS_POST){
      //微信支付1,支付宝为0
      $type=I('post.type');
      if($type == 1){
          header('Content-type:text/html;charset=utf-8');
          header('Location:http://www.nbtjf.com/wxpay/example/jsapi.php');
      }else{
        //支付宝支付

      }
    }
    return $this->fetch();
  }

 /**
 *
 *约车
 */
 public function yueche()
 {
 
  return $this->fetch();
 }

 public function getche()
 {
  $data['loupan']=I('post.loupan');
  $data['yongchetime']=I('post.yongchetime');
  $data['shangche']=I('post.shangche');
  $data['wtime']=I('post.wtime');
  $info = M('message')->where('message_type=3')->add($data);
  if($info){
    echo json_decode(1);
  }else{
    echo json_decode(2);
  }
  
 }

//*****************************************会员中心**************************************
  /**
  * 会员中心
  **/
  public function member()
  {
    $where['id']=session('mid');
    $info = M('member')->where($where)->find();
    $this->assign('info',$info);
    return $this->fetch();
  }

 public function  login()
 {
    if(IS_POST){
        $where['phone'] =trim($_POST['phone']);
        $where['pwd'] = md5($_POST['pwd']);
        $info = M('member')->where($where)->find();
        if($info){
             session('mid',$info['id']);
             session('phone',$info['phone']);
             session('uname',$info['uname']);
             return $this->redirect('Index/member');
        }else{

          return $this->redirect('Index/login');
        }
  }

   return $this->fetch();
 }
 //快速登录
 public function ks_login()
 {
   if(!empty($_POST['phone']) && !empty($_POST['yanzheng']) ){
        $session= session('ks_login_yzm');
        $getyan = I('post.yanzheng');
        $phone = I('post.phone');
        $check = M('member')->where('phone='.$phone)->find();
           if ( ($session == $getyan) && $check){
               session('mid',$check['id']);
               session('phone',$check['phone']);
               session('uname',$check['uname']); 
               return $this->redirect('Index/member');     
           }else{
             return $this->redirect('Index/login');       
           }
      }else{
       return $this->redirect('Index/login'); 
      }
          
 }
 public function get_ks_login()
 {
  //快速登录,如果手机号存在就登录，不存在就注册后登录
   session('ks_login_yzm',random(6));
    $path ="modules/sendmsg.php";
    $jiekou = unserialize(file_get_contents($path));   
      $uid = $jiekou['uid'];
      $pwd = $jiekou['pwd'];
      $mobile  = $_POST['phone'];
      $content = $jiekou['content'].session('ks_login_yzm'); 
      $sendMsg = new \org\Sendmsg();
    //  $sendMsg->sendSMS($uid,$pwd,$mobile,$content);

 }

 public function login_out()
 {
    session('mid',null);
    return $this->redirect('Index/member');
 }

 public function register()
 {
  //如果验证码通过，手机号唯一即可写入数据库
    $yzm = session('yzm');
    $this->assign('error',0);
  if(IS_POST){
      $toyzm = $_POST['yzm'];
      $phone = I('post.phone');
      $pwd = md5(I('post.pwd'));
      $check = M('member')->where('phone='.$phone)->find();
       //如果存在就弹出错误提示
       if($check){
           $error = 1;
           $this->assign('error',$error);
       }

      if($yzm == $toyzm && !$check){
         $data['phone']=$phone;
         $data['pwd']=$pwd;        
         $info = M('member')->add($data);
          if($info){
            $check = M('member')->where('phone='.$phone)->find();
             session('mid',$check['id']);
             session('phone',$check['phone']);
             session('uname',$check['uname']);            
             return $this->redirect('Index/member');
          }
      }
  }
   
   return $this->fetch();
 }

//忘记密码
 public function forget()
 {
  //首先，验证是否有这个手机号。如果有，手机号码获取验证码后，
  //将此条密码进行修改,因为判断了验证码，这里直接保存
  if(IS_POST){
    $where['phone']=I('post.phone');
    $data['pwd'] = md5($_POST['pwd']);
    $result = M('member')->where($where)->save($data);
     if(false !== $result || 0 !== $result){
       $info = M('member')->where($where)->find();
       session('mid',$info['id']);
       session('phone',$info['phone']);
       session('uname',$info['uname']);  
       }
     return $this->redirect('Index/member');
  }

  return $this->fetch();
 }
 public function f_info()
 { 
      $where['phone']=I('post.phone');
      $info = M('member')->where($where)->count();
      //如果为0 ，则说明未注册.否则已注册
       if($info ==0){
        echo 1;
       }else{
      //将验证码存到session
        $path ="modules/sendmsg.php";
        $jiekou = unserialize(file_get_contents($path));     
        session('forget_yzm',random(6));
        $uid = $jiekou['uid'];
        $pwd = $jiekou['pwd'];
        $mobile  = $_POST['phone'];
        $content = $jiekou['content'].session('forget_yzm'); 
        $sendMsg = new \org\Sendmsg();
        echo session('forget_yzm');
       // $sendMsg->sendSMS($uid,$pwd,$mobile,$content);
       }

 }
 //实时验证忘记密码的验证码
 public function check_f_info()
 {
    if(IS_POST){
      $syzm = session('forget_yzm');
      $get_check = I('post.yzm');
      if($syzm == $get_check){
        echo 1;
      }else{
        echo 0;
      }
    }
 }

//账户
 public function zhanghu()
 {
  $smid= session('mid');
  $old = M('member')->where('id='.$smid)->find();
  $this->assign('info',$old);
  return $this->fetch();
 }
 //修改头像
 public function edit_headimg()
 {
  $smid= session('mid');
  $data['headimg']=$_POST['img'];
  $info = M('member')->where('id='.$smid)->save($data);

 }
//修改昵称
 public function edit_uname()
 {
  $smid= session('mid');
  $old = M('member')->where('id='.$smid)->find();
  if(IS_POST){
      $name=I('post.uname');
      $uname = !empty($name)?$name:"未填写";
      $data['uname'] = $uname;
      session('uname',$uname);
      $info = M('member')->where('id='.$smid)->save($data);
      return $this->redirect('Index/member'); 
  }  
  $this->assign('info',$old['uname']);
  return $this->fetch();
 }
//修改手机号
 public function edit_phone()
 {
   if(IS_POST){
     if($_POST['type'] == 1){
        $path ="modules/sendmsg.php";
        $jiekou = unserialize(file_get_contents($path));
        session('edit_phone',random(6));
        $uid = $jiekou['uid'];
        $pwd = $jiekou['pwd'];
        $mobile  = $_POST['phone'];
        $content = $jiekou['content'].session('edit_phone'); 
        $sendMsg = new \org\Sendmsg();
        //$sendMsg->sendSMS($uid,$pwd,$mobile,$content); 
        echo  session('edit_phone');        
        return;
     }
   }
  return $this->fetch();
 }
 //后台验证短信验证码
 public function check_edit_phone()
 {
    if(IS_POST){
      $syzm = session('edit_phone');
      $get_check = I('post.yzm');
      if($syzm == $get_check){
        echo 1;
      }else{
        echo 0;
      }
    }

 }
 //新增手机号
 public function edit_phone_add()
 {
    $smid= session('mid');
     if(IS_POST){
       if($_POST['type'] == 1){
          $path ="modules/sendmsg.php";
          $jiekou = unserialize(file_get_contents($path));
          session('edit_phone_add',random(6));
          $uid = $jiekou['uid'];
          $pwd = $jiekou['pwd'];
          $mobile  = $_POST['phone'];
          $content = $jiekou['content'].session('edit_phone_add'); 
          $sendMsg = new \org\Sendmsg();
        // $sendMsg->sendSMS($uid,$pwd,$mobile,$content); 
          echo  session('edit_phone_add');        
          return;
       }
     }
  return $this->fetch();
 }
 //实时验证,新增手机
 public function check_edit_phone_add()
 {
      if(IS_POST){
      $syzm = session('edit_phone_add');
      $get_check = I('post.yzm');
      if($syzm == $get_check){
        echo 1;
      }else{
        echo 0;
      }
    } 
 }

 public function get_phone()
 { 
      $smid= session('mid');
      $phone = I('post.phone');
      $where['id']=$smid;
      $data['phone']=$phone;
      $info = M('member')->where($where)->setField('phone',$phone);
      if($info){
        session('phone',$phone);
        return $this->redirect('Index/member'); 
      }
 }
//修改密码
 public function edit_pwd()
 {

  $smid= session('mid');
  $old = M('member')->where('id='.$smid)->find();
  if(IS_POST){
    $repwd = md5(trim(I('post.repwd')));
      if($repwd == $old['pwd']){
          $data['pwd'] = md5(trim(I('post.pwd')));
          M('member')->where('id='.$smid)->save($data);
      }else{
          $this->assign('error',0);
      }

  }
      
      return $this->fetch();
 }

 public function yanzheng()
 {
    $path ="modules/sendmsg.php";
    $jiekou = unserialize(file_get_contents($path));
    $yz = $_POST['yz'];
   if($yz == "1"){
      session('yzm',random(6));
      $uid = $jiekou['uid'];
      $pwd = $jiekou['pwd'];
      $mobile  = $_POST['phone'];
      $content = $jiekou['content'].session('yzm'); 
      $sendMsg = new \org\Sendmsg();
      $sendMsg->sendSMS($uid,$pwd,$mobile,$content);
      echo session('yzm');
   }
 }
  /**
  *降价通知与开盘通知
  *
  **/
 public function notice()
 {
    $path ="modules/sendmsg.php";
    $jiekou = unserialize(file_get_contents($path));
    $type =$_POST['type'];
    //type=1 为降价通知，type=2，为开盘通知,此处用于发送验证码
    if($type == 1){
      session('jiangjia_yzm',random(6));
      $uid = $jiekou['uid'];
      $pwd = $jiekou['pwd'];
      $mobile  = $_POST['phone'];
      $content = $jiekou['content'].session('jiangjia_yzm'); 
      $sendMsg = new \org\Sendmsg();
      $sendMsg->sendSMS($uid,$pwd,$mobile,$content);      
    }elseif($type == 2){
      session('tongzhi_yzm',random(6));
      $uid = $jiekou['uid'];
      $pwd = $jiekou['pwd'];
      $mobile  = $_POST['phone'];
      $content = $jiekou['content'].session('tongzhi_yzm'); 
      $sendMsg = new \org\Sendmsg();
      $sendMsg->sendSMS($uid,$pwd,$mobile,$content);       
    }

 }

 public function add_jiangjia($id)
 {

   if(IS_POST){
      $yanzm = session('jiangjia_yzm');
       if($yanzm == $_POST['yanzhengma']){
        $data['panid']=$id;
        $data['message_type']=4;
        $data['phone']=$_POST['phone'];
         M('message')->add($data);
         return $this->redirect('Index/loupan_content',array('id'=>$id));
       }
   }
 }
 public function check_add_jiangjia()
 {
      if(IS_POST){
        $syzm = session('jiangjia_yzm');
        $get_check = I('post.yzm');
        if($syzm == $get_check){
          echo 1;
        }else{
          echo 0;
        }
      }
 }

 public function add_kaipan($id)
 {
   if(IS_POST){
      $yanzm = session('tongzhi_yzm');
       if($yanzm == $_POST['yanzhengma']){
        $data['panid']=$id;
        $data['message_type']=5;        
        $data['phone']=$_POST['phone'];
        M('message')->add($data);
         return $this->redirect('Index/loupan_content',array('id'=>$id));
       }
   }
 }

 public function check_add_kaipan()
 {
      if(IS_POST){
        $syzm = session('tongzhi_yzm');
        $get_check = I('post.yzm');
        if($syzm == $get_check){
          echo 1;
        }else{
          echo 0;
        }
      }
 }
  /**
  *降价通知与开盘通知 END
  *
  **/

 public function fangkuan_list()
 {
   $this->isLogin();
    $his=session('mid');
   if($his){
    $list = M('coupon')->where('his='.$his)->order('get_time desc')->select();
  }else{
    $list=array();
  }
   
   $this->assign('list',$list);
   return $this->fetch();
 }

 public function fangkuan_content($id)
 {
   $this->isLogin();
  $info = M('coupon')->where('id='.$id)->find();
  $this->assign('info',$info);
  return $this->fetch();
 }

/**
* concern 关注
**/
public function concern()
{
  $this->isLogin();
  if(session('mid')){
    //查询concern表中是否有这个userid
     $member_id = session('mid');
     $map['userid']=$member_id;
        $str= session('str_guanzhu_id'.session('mid'));
        if($str){
          $str= session('str_guanzhu_id'.session('mid'));
        }else{
          $str=0;
        }
       
        $find = M('concern')->where($map)->find();
         if(!$find){
             $data['userid']=$member_id;
             $data['hid']= session('str_guanzhu_id'.session('mid'));
             M('concern')->add($data);
         }else{
            $map['userid']=$member_id;
            $puthistory = M('concern')->where($map)->setField('hid',$str);          
         }
        //关注列表
        $info = M('concern')->where($map)->getfield('hid');
        $where['id']=array('in',$info);
        $concern = M('term')->where($where)->select();
          if($concern){
            $list = $this->json_array($concern,count($concern)-1); 
          }else{
            $list = array(); 
          }
        
        $this->assign('list',$list);
    }else{
      $info =array();
      $this->assign('list',$info);
    }
    return $this->fetch();
}

public function get_concern()
{
  //把所有关注的id传到数据库，判断这个id是否在hid中，如果在就显示红色，不在现实灰色
    $useid = session('mid');
    $guanzhu = $_POST['guanzhu_id'];
    $status  = $_POST['status'];
    session('guanzhu_id'.$_POST['guanzhu_id'],$_POST['guanzhu_id']);
    if($status == 0){
        session('guanzhu_id'.$_POST['guanzhu_id'],null);  
    }

    $str="";
      for($i=1;$i<=30;$i++){
        if(session('guanzhu_id'.$i) != null){
          $str.= session('guanzhu_id'.$i).",";    
         }
      }
    session('str_guanzhu_id'.$useid,$str);
  
    $map['userid']=session('mid');
    $str= session('str_guanzhu_id'.$useid);
   //如果存在指定的会员，则进行更新，否则，先创建在更新
    $find = M('concern')->where('userid='.$useid)->find();
    if($find){
        M('concern')->where('userid='.$useid)->setField('hid',$str); 
      }else{
        $data['userid']=$useid;
        $data['hid']=$str;
        M('concern')->add($data);
      }
    //时时关注状态
     if($status ==1){
       echo json_decode('1');
     }else{
       echo json_decode('0');
     }
    
}

 public function history()
 {

 $this->isLogin();
  //只记录浏览的楼盘，通过获取id，再与term表中查找，浏览loupan_content一次记录一次。形成浏览记录
  if(session('mid')){
        //查询history表中是否有这个userid
        $str= session('str_id'.session('mid'));
        if($str){
          $str= session('str_id'.session('mid'));
        }else{
          $str=0;
        }
        $member_id =session('mid');
        $find['userid']=$member_id;
        $fmember= M('history')->where($find)->find();
        if(!$fmember){
           $data['userid']=$member_id;
           $data['hid']= $str;
           M('history')->add($data);
        }else{
          //存在则向数据库写入浏览历史
          $map['userid']=session('mid');
          $puthistory = M('history')->where($map)->setField('hid',$str);     
        }

  
        //历史列表页

          $info = M('history')->where($find)->getfield('hid');
          $where['id']=array('in',$info);
          $history = M('term')->where($where)->select();
            if($history){
              $list = $this->json_array($history,count($history)-1); 
            }else{
              $list=array();
            }
             $this->assign('list',$list);
        }else{
          $info =array();
          $this->assign('list',$info);
        }

      return $this->fetch();
 }

 public function yijian()
 {
   $this->isLogin();
  if(IS_POST){
    $data['mid']=I('post.mid');
    $data['message_type']=I('post.type');
    $data['content'] = I('post.msg');
    $data['connect'] = I('post.connect');
    $info = M('message')->add($data);
    if($info){
      return $this->redirect('Index/member');
    }else{
      return $this->redirect('Index/yijian');
    }
  }
  return $this->fetch();
 }

public function dianpin()
{
   $this->isLogin();
  $where['mid']=session('mid');
  $where['message_type']=1;
  $list = M('message')->where($where)->select();
  $this->assign('list',$list);
  return $this->fetch();
}
 
//***************************************会员中心 END***********************************

  public function shaixuan()
  {
    $where['parent_id'] =$_POST['id'];
    $erji = M('term')->where($where)->select();
    echo json_encode($erji);
  }
  public function shaixungengduo()
  {
    $where['lid'] =$_POST['lid'];
    $gengduo = M('links')->where($where)->select();
    echo json_encode($gengduo);
  }

  public function sxjunjia()
  {
    $search=$_POST['junjia'];
    // $str =preg_replace('/元.*/i',"",$search);
    //如果出现 > 或 < 符号，则只取数字.如果出现-符号，则分割成一个最低和一个最高价格
    //正则表达式的效率是很低的,能不用正则就不要用,切记!
    $str =strstr($search,"元",true);//返回元字之前的所有字符串
    //判断是否出现-符号,出现返回位数，否则返回false。用 “===”进行判断
    $panduan = strpos($str,'-');
    $panxiaoyu = strpos($str,'<');
    $pandayu = strpos($str,'>');
    $where['display_nav']=1;
    //如果存在- 符号,就取前一部分，和后一部分   
    if($panduan !==false){
     $qian = strstr($str,"-",true);
     $hou = str_replace("-","",$str);       
     $where['junjia']=array(array('egt',(int)$qian),array('lt',(int)$hou));
    }else if($panxiaoyu !==false){
      //判断是否出现小于<符号,小于小的
      $xiao = str_replace("<","",$str);
      $where['junjia']=array(array('lt',$xiao));
    }else if($pandayu !==false){
      $da = str_replace(">","",$str);
      $where['junjia']=array(array('egt',$da));      
    }

    $list = $this->term->where($where)->select();
    if(!empty($list)){
      $new_array = $this->json_array($list,count($list)-1);   
    }else{
      $new_array = $this->term->where($where)->select();  
    } 
  $this->assign('list',$new_array);      
      return $this->fetch('Index/loupan_list');
  }

  public function sx(){
    if($_POST['zi'] ==="不限"){
      $map['display_nav'] =1; 
    }else{
      $search = $_POST['zi'];
      $map['display_nav'] =1;  
      $where['area']=array('like',"%$search%");
      $where['junjia']=array('like',"%$search%");
      $where['huxing']=array('like',"%$search%");
      $where['kaipan']=array('like',"%$search%");
      $where['fangtype']=array('like',"%$search%");
      $where['chanquan']=array('like',"%$search%");
      $where['_logic'] = 'or'; 
      $map['_complex'] = $where;
    }
    $list = $this->term->where($map)->select();
    if(!empty($list)){
      $new_array = $this->json_array($list,count($list)-1);   
    }else{
      $new_array = $this->term->where($map)->select();  
    } 
  $this->assign('list',$new_array);
  return $this->fetch('Index/loupan_list');
  }
	/**
	 * 搜索页
	 */
    public function search()
    {
      $search = $_POST['search'];
      //注意这个顺序，不行就打印出sql语句
      $map['display_nav'] = array('eq',1);  
      $where['area']=array('like',"%$search%");
      $where['junjia']=array('like',"%$search%");
      $where['huxing']=array('like',"%$search%");
      $where['_logic'] = 'or'; 
      $map['_complex'] = $where; 
      $list = $this->term->where($map)->select();
          if(!empty($list)){
            $new_array = $this->json_array($list,count($list)-1);   
          }else{
            $new_array = $this->term->where($map)->select();  
          } 
        $this->assign('list',$new_array);
        return $this->fetch('Index/loupan_list');
    }
	


   /**
   *  合并json转换成的数组，得到新数组，从而实现自定义标签
   */
	
	private function json_array($array,$count=1)
	{
     if($array){
    		if(!isset($array[0])){
    	        $jsonToarray = json_decode($array['tags_value'],true);
    		    $new_array = array_merge($array,$jsonToarray);			
    		}else{
    			for($i=0;$i<=$count;$i++){
                    $jsonToarray = json_decode($array[$i]['tags_value'],true);
    	           $new_array[] = array_merge($array[$i],$jsonToarray);					
    			}
    		}
    		return $new_array;
      }else{
        $new_array=array();
        return $new_array;
      }
	}

	
   /**
   *  二维数组转一维数组
   */	
	
	private function TwoToOne($array,$v)
	{
		  $new=[];
       foreach($array as $key=>$value){
       	  $new[] = $value[$v];
       }
       return $new;
	}
	
 /**
 *对会员是否登录进行验证
 *
 **/
 private function isLogin()
 {
    if(!session('mid')){
        return $this->redirect('Index/login');
      }
      return false;
 }
	
	


	


	
}

